package com.jwgf.smarowindingmachine_unity.ui;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;

import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

import com.jwgf.common.ViewCache;
import com.jwgf.queueprogramflash.FlashManager;
import com.jwgf.smarowindingmachine_unity.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class EquipmentMaintenanceSpindleUpgradeFragment extends BaseFragment {

    private TextView range_text;
    private Button select_spindle_btn;
    private TextView file_path;
    private Button select_btn;
    private Button flash_btn;
    private TextView tv;
    //    private FlashManager flashManager;
    private LinkedList<String> displayContent;

    private ArrayList<Integer> selectSpindleList = new ArrayList();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = ViewCache.getInstance().getView(R.layout.fragment_equipment_maintenance_spindle_upgrade);
        range_text = v.findViewById(R.id.range_text);
        select_spindle_btn = v.findViewById(R.id.select_spindle);
        file_path = v.findViewById(R.id.file_path);
        select_btn = v.findViewById(R.id.select_file);
        flash_btn = v.findViewById(R.id.flash);
        tv = v.findViewById(R.id.tv);
        tv.setMovementMethod(new ScrollingMovementMethod());
        displayContent = new LinkedList<>();
        select_spindle_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SpindleSelectedDialog dialog = new SpindleSelectedDialog();
                Runnable callback = new Runnable() {
                    @Override
                    public void run() {
                        selectSpindleList.clear();
                        List<Integer> spindles = dialog.getSpindleList();
                        for (Integer spindle : spindles) {
                            selectSpindleList.add(spindle + 1);
                        }
                        range_text.setText(spindles.toString());
                    }
                };
                dialog.setCallBack(callback);
                dialog.show(getFragmentManager(), "select_spindle");
            }
        });
        select_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] content = getExternalDevices();
                if (content == null) {
                    return;
                }
                ListDialogFragment listDialog = new ListDialogFragment();
                Bundle bd = new Bundle();
                bd.putStringArray(ListDialogFragment.ARRAY, content);
                listDialog.setArguments(bd);
                listDialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        final FileSelectedDialog fileSelectedDlg = new FileSelectedDialog();
                        Bundle bd = new Bundle();
                        bd.putString(FileSelectedDialog.ROOT_PATH, content[position]);
                        fileSelectedDlg.setArguments(bd);
                        fileSelectedDlg.setCallBack(new Runnable() {

                            @Override
                            public void run() {
                                file_path.setText(fileSelectedDlg.getPath());
                            }
                        });
                        fileSelectedDlg.show(getFragmentManager(), "fileSelected");
                    }
                });
                listDialog.show(getFragmentManager(), "listDialog");
            }
        });
        flash_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                select_spindle_btn.setEnabled(false);
                                flash_btn.setEnabled(false);
                                select_btn.setEnabled(false);
                            }
                        });
                        displayContent.clear();
                        FlashManager.getInstance().startFlash(file_path.getText().toString(), selectSpindleList, new FlashManager.FlashStateListener() {
                            @Override
                            public void onFlashStateChanged(String msg) {
                                displayContent.add(msg);
                                updateTextView();
                            }
                        });
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                select_spindle_btn.setEnabled(true);
                                flash_btn.setEnabled(true);
                                select_btn.setEnabled(true);
                            }
                        });
                    }
                }).start();

            }
        });
        return v;
    }

    private void updateTextView() {
        StringBuilder sb = new StringBuilder();
        for (String s : displayContent) {
            sb.append(s).append("\n");
        }
        tv.setText(sb.toString());
    }

    private String[] getExternalDevices() {
        ArrayList<String> pathList = new ArrayList<String>();
        String path;
        if (Build.VERSION.RELEASE.equals("6.0.1")) {
            path = "/mnt/media_rw/usb1";
        } else {
            path = "/mnt/usb1";
        }

        File file1 = new File(path);
        if (file1.exists()) {
            if (file1.list().length > 0) {
                pathList.add(path);
            }

            if (pathList.size() != 0) {
                return pathList.toArray(new String[]{});
            } else {
                return null;
            }
        }
        return null;

    }
}
